package csc.fresher.service.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.constant.ProjectBankingConstant;
import csc.fresher.dao.CustomerAccountDao;
import csc.fresher.dao.SavingAccountTransactionDao;
import csc.fresher.entity.AccountHasInterestRate;
import csc.fresher.entity.AccountHasInterestRateState;
import csc.fresher.entity.SavingAccountHasInterestRate;
import csc.fresher.entity.SavingTransaction;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.entity.Transaction;
import csc.fresher.entity.TransactionState;
import csc.fresher.entity.TransactionType;
import csc.fresher.factory.CustomerAccountDaoFactory;
import csc.fresher.service.SavingTransactionService;

@Service
public class SavingTransactionServiceImpl implements SavingTransactionService {
	
	@Autowired
	CustomerAccountDaoFactory customerAccountDaoFactory;

	@Autowired
	private SavingAccountTransactionDao savingAccountTransactionDao;
	
	private CustomerAccountDao customerAccountDao;

	@PostConstruct
	public void init() {
		customerAccountDao = customerAccountDaoFactory.getType(ProjectBankingConstant.SAVING_ACCOUNT_TYPE);
	}

	@Override
	public boolean checkActiveCustomerAccountTransactionState(long accountNumber){
		return customerAccountDao.checkActiveCustomerAccountTransactionState(accountNumber);
	}
	
	@Override
	public long createTransaction(double funds,  long savingAccountNumber, double beforeTxBalance,
			int createdBy,SavingTransactionType savingTransactionType, int interestRateID, Timestamp fromDate, Timestamp toDate) {

		if(funds == 0){
			return -2;
		}
		boolean isActive = customerAccountDao.checkActiveCustomerAccountTransactionState(savingAccountNumber);
		if(!isActive){
			return ProjectBankingConstant.ACCOUNT_NOT_ACTIVE;
		}
		
		
		SavingTransaction savingTransaction = new SavingTransaction(funds, fromDate, toDate, beforeTxBalance, createdBy, 1, savingTransactionType, savingAccountNumber);
		
		double beforeInterestBalance = 0;
		if(savingTransactionType.getType().equals("Saving")){
			beforeInterestBalance = beforeTxBalance + funds;
		}else if(savingTransactionType.getType().equals("WithdrawInterest")){
			beforeInterestBalance = beforeTxBalance - funds;
		}
		Calendar cal = Calendar.getInstance();       // get calendar instance
		cal.setTimeInMillis(fromDate.getTime());     // set cal to date
		cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
		cal.set(Calendar.MINUTE, 0);                 // set minute in hour
		cal.set(Calendar.SECOND, 0);                 // set second in minute
		cal.set(Calendar.MILLISECOND, 0);            // set millis in second
		Timestamp fromDateT = new Timestamp(cal.getTimeInMillis());
		cal.setTimeInMillis(toDate.getTime());     // set cal to date
		cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
		cal.set(Calendar.MINUTE, 0);                 // set minute in hour
		cal.set(Calendar.SECOND, 0);                 // set second in minute
		cal.set(Calendar.MILLISECOND, 0);            // set millis in second
		Timestamp toDateT = new Timestamp(cal.getTimeInMillis());
		
		AccountHasInterestRate accountHasInterestRate = new SavingAccountHasInterestRate(savingAccountNumber, interestRateID, fromDateT, toDateT, beforeInterestBalance, AccountHasInterestRateState.PENDING);
		
		long transactionID = savingAccountTransactionDao.createTransaction(savingTransaction, accountHasInterestRate);
		//JOptionPane.showMessageDialog(null, "ok!");
		return transactionID;
		
	}

	@Override
	public boolean approveTransactionState(long transactionID,
			long accoutNumberID, long systemUserID) {
		return savingAccountTransactionDao.approveTransaction(transactionID, systemUserID, accoutNumberID);
	}

	@Override
	public boolean denyTransactionState(long transactionID,
			long accoutNumberID, long systemUserID) {
		return savingAccountTransactionDao.denyTransaction(transactionID, systemUserID, accoutNumberID);
	}

	@Override
	public List<Transaction> getListTransactionAccountInAPeriod(
			long accountNumberID, Timestamp begin, Timestamp end,
			int startIndex, int maxReturn) {
		List<Transaction> transactions = new ArrayList<Transaction>();
		transactions = savingAccountTransactionDao.getListTransactionsByAccountIDInGivenPeriod(accountNumberID, begin, end, startIndex, maxReturn);
		return transactions;
	}

	@Override
	public List<Transaction> getListTransactionAccountInAPeriodByType(
			long accountNumberID, Timestamp begin, Timestamp end,
			TransactionType transactionType, int startIndex, int maxReturn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Transaction> getListTransactionAccountInAPeriodByState(
			long accountNumberID, Timestamp begin, Timestamp end,
			TransactionState transactionState, int startIndex, int maxReturn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Transaction> getListInterestRateOfUser(long accountNumberID,
			int startIndex, int maxReturn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Transaction> getListSavingTransactionByAccountNumber(
			long accountNumber) {
		List<Transaction> transactions = null;
		if(savingAccountTransactionDao != null){
			transactions = savingAccountTransactionDao.searchTransactionByAccountID(accountNumber);
		}
		return transactions;
	}

	
	@Override
	public List<Transaction> getListTransactionAccountInAPeriod(
			long accountNumberID, Timestamp begin, Timestamp end) {
		List<Transaction> transactions = null;
		if(savingAccountTransactionDao != null){
			transactions = savingAccountTransactionDao.getAllTransactionsByAccountIDInGivenPeriod(accountNumberID, begin, end);
		}
		return transactions;
	}

	@Override
	public List<Transaction> getListTransactionAccountAll() {
		List<Transaction> transactions = null;
		if(savingAccountTransactionDao != null){
			transactions = savingAccountTransactionDao.getAllSavingTransaction();
		}
		return transactions;
	}
	@Override
	public List<Transaction> getListTransactionAccountIsApprove(){
		List<Transaction> transactions = null;
		if(savingAccountTransactionDao != null){
			transactions = savingAccountTransactionDao.getSavingTransactionIsApproved();
		}
		return transactions;
	}
	@Override
	public List<Transaction> getListTransactionAccountIsPending(){
		List<Transaction> transactions = null;
		if(savingAccountTransactionDao != null){
			transactions = savingAccountTransactionDao.getSavingTransactionIsPending();
		}
		return transactions;
	}
}
